package com.payease.ezt.activity;

import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.payease.ezt.receiver.NetStateReceiver;
import com.payease.ezt.R;

public class AboutUsActivity extends BaseActivity implements View.OnClickListener ,NetStateReceiver.NetworkStateListener {
    private Toolbar toolbar;
    private TextView tvTitle;

    private TextView tvPayease;
    private TextView tvSilkroadTrad;
    private TextView tvDoku;
    private TextView tvEspay;
    private TextView tvFinnet;

    private String payeaseUrl;
    private String silkroadTradUrl;
    private String dokuUrl;
    private String espayUrl;
    private String finnetUrl;

    private boolean isNotNetworkEnter;
    private NetStateReceiver receiver;
    private boolean isNetworkConnected;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);

        initData();

        initToolBar();

        initViews();

        receiver = new NetStateReceiver();
        receiver.setNetWorkStateListener(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver,intentFilter);
    }

    private void initData() {
        payeaseUrl = getString(R.string.beijing);
        silkroadTradUrl = getString(R.string.silkroad_tradease);
        dokuUrl = getString(R.string.doku);
        espayUrl = getString(R.string.espay);
        finnetUrl = getString(R.string.finnet);

        isNotNetworkEnter = getIntent().getBooleanExtra("isNotNetworkEnter", false);
    }

    private void initViews() {
        tvPayease = (TextView) findViewById(R.id.tv_payease);
        tvSilkroadTrad = (TextView) findViewById(R.id.tv_silk_road_trad);
        tvDoku = (TextView) findViewById(R.id.tv_doku);
        tvEspay = (TextView) findViewById(R.id.tv_espay);
        tvFinnet = (TextView) findViewById(R.id.tv_finnet);

        tvSilkroadTrad.setOnClickListener(this);
        tvPayease.setOnClickListener(this);
        tvDoku.setOnClickListener(this);
        tvEspay.setOnClickListener(this);
        tvFinnet.setOnClickListener(this);

    }

    private void initToolBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tvTitle = (TextView) findViewById(R.id.toolbar_title);

        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tvTitle.setText(R.string.about_us);
    }

    private void displayInSelf(String url) {
        Intent intent = new Intent(this,AboutDetailActivity.class);
        intent.putExtra("url",url);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        if (!isNetworkConnected){
            showMsg(getString(R.string.network_disconnected));
            return;
        }
        int id = v.getId();
        switch (id) {
            case R.id.tv_payease:
                displayInSelf(payeaseUrl);
                break;
            case R.id.tv_silk_road_trad:
                displayInSelf(silkroadTradUrl);
                break;
            case R.id.tv_doku:
                displayInSelf(dokuUrl);
                break;
            case R.id.tv_espay:
                displayInSelf(espayUrl);
                break;
            case R.id.tv_finnet:
                displayInSelf(finnetUrl);
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    public void onStateChange(NetworkInfo.State state) {
        switch (state){
            case CONNECTED:
                if (isNotNetworkEnter){
                    finish();
                }else{
                    isNetworkConnected = true;
                }
                break;

            case DISCONNECTED:
                isNetworkConnected = false;
                break;
        }
    }
}
